import api from '@/axios.js';
import {ElMessage} from "element-plus";
import router from "@/routers/index.js";

export function register(userData){
        api.post('/users/register', userData).then(
            (response) => {
                if (response.code === 200) {
                    console.log(response.msg);
                    ElMessage.success("注册成功");
                    router.push('/login');
                } else {
                    ElMessage.error("注册失败: " + response.msg);
                }
            }
        ).catch(
            (error) => {
                console.error("注册请求失败: ", error);
                ElMessage.error("注册请求失败: " + error.msg);
            }
        )
}
export function getUserByUsername(username) {
    return api({
        method: 'get',
        url: '/users/getUserByUsername',
        params: { username }
    })
        .then(response => {
            return response.data; // 确保返回数据
        })
        .catch(error => {
            ElMessage.error("获取用户信息失败: " + error.message);
            throw error; // 重新抛出错误以便调用者处理
        });
}